package com.moyun.zbmy.zizhou.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;

import com.moyun.zbmy.zizhou.application.Cache;
import com.moyun.zbmy.zizhou.application.Constances;
import com.moyun.zbmy.zizhou.beans.AccountCommentBean;
import com.moyun.zbmy.zizhou.beans.AttentionBean;
import com.moyun.zbmy.zizhou.beans.CommentTemp;
import com.moyun.zbmy.zizhou.beans.SingleResult;
import com.moyun.zbmy.zizhou.http.AbsNetCallBack;
import com.moyun.zbmy.zizhou.http.NetUtils;
import com.moyun.zbmy.zizhou.ui.dialog.CommentAccountDiaFragment;
import com.moyun.zbmy.zizhou.ui.utils.DataUtils;
import com.moyun.zbmy.zizhou.ui.utils.SkipUtil;
import com.moyun.zbmy.zizhou.utils.UserManager;
import com.ruihang.generalibrary.ui.dialog.ShareFragment;
import com.ruihang.generalibrary.utils.IShares;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;

/**
 * 华栖云融媒相关 基础Activity
 */
public class BaseAccountActivity extends BaseCanPlayActivity {

    protected CommentAccountDiaFragment commentDiaFragment;
    protected ShareFragment shareFragment;
    public static final String SHARE_TAG = "share_fragment";
    public static final String COMMENT_TAG = "dia_comment_fragment";
    protected Handler mHandler;
    private String newsId;
    private int typeName = 0;//新闻、直播类型

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void toAttentionAccount(String accountId, final OnAttentionListener listener) {
        if (null != UserManager.getInstance().getUser()) {
            String token = UserManager.getInstance().getUser().getToken();
            String userId = UserManager.getInstance().getUser().getUserId();
            Map<String, Object> param = new HashMap<>();
            param.put("siteNumber", Constances.SITE_ID);
            param.put("token", token);
            param.put("userId", userId);
            param.put("followedId", accountId);

            NetUtils.getNetAdapter().addAttentionAccount(getOwnerName(), param, new AbsNetCallBack<String>(String.class) {
                @Override
                public void onSuc(String result) {
                    if (null != listener) {
                        toast("关注成功");
                        listener.OnAttention(true, 0);
                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
//                    toast(errorMsg);
                    if (null != listener) {
                        toast("关注失败");
//                        listener.OnAttention(false, 0);
                    }
                }

                @Override
                public void onEnd() {
                    super.onEnd();
                }
            });
        }else{
            toLogin();
        }


    }

    public void toUnAttentionAccount(String accountId, final OnAttentionListener listener) {
        if (null != UserManager.getInstance().getUser()) {
            String token = UserManager.getInstance().getUser().getToken();
            String userId = UserManager.getInstance().getUser().getUserId();
            Map<String, Object> param = new HashMap<>();
            param.put("siteNumber", Constances.SITE_ID);
            param.put("token", token);
            param.put("userId", userId);
            param.put("followedId", accountId);

            NetUtils.getNetAdapter().deleteAttentionAccount(getOwnerName(), param, new AbsNetCallBack<String>(String.class) {
                @Override
                public void onSuc(String result) {
                    if (null != listener) {
                        toast("取消关注成功");
                        listener.OnUnAttention(true, 0);

                    }
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
//                    toast(errorMsg);
                    if (null != listener) {
                        toast("取消关注失败");
//                        listener.OnUnAttention(false, 0);
                    }
                }

                @Override
                public void onEnd() {
                    super.onEnd();
                }
            });
        }else{
            toLogin();
        }

    }

    private AccountCommentBean commentContent;

    /**
     * 添加pingl
     *
     * @param comment
     * @param flag
     * @param listener
     */
    protected synchronized void showCommentDialog(AccountCommentBean comment, final int flag,
                                                  final OnAddCommentListener listener) {
        this.commentContent = comment;
        FragmentManager fm = getSupportFragmentManager();
//        if (commentDiaFragment == null) {
        commentDiaFragment = new CommentAccountDiaFragment();
        commentDiaFragment.setSendComment(new CommentAccountDiaFragment.SendCommentListener() {
            @Override
            public void onCommentSend(String content) {
                commentContent.setContent(content);
                toAddComment(commentContent, flag, listener);
            }
        });
        commentDiaFragment.show(fm, COMMENT_TAG);
        fm.executePendingTransactions();
    }

    /**
     * 添加评论、回复评论
     *
     * @param commentContent 评论内容不符
     * @param flag           0：添加评论 1：添加评论回复
     * @param listener
     */
    public void toAddComment(AccountCommentBean commentContent, int flag,
                             final OnAddCommentListener listener) {
        Map<String, String> formMaildata = new HashMap<>();
        formMaildata.put("relaId", commentContent.getRelaId());
        formMaildata.put("content", commentContent.getContent());
//            formMaildata.put("nickName", commentContent.getNickName());
//            formMaildata.put("avatar", commentContent.getAvatar());
        formMaildata.put("type", commentContent.getType());//1：文稿评论 2：活动 3：评论回复
        formMaildata.put("uId", commentContent.getAddUser());
        formMaildata.put("flag", "" + flag);//评论标识 0：添加评论 1：添加评论回复
//            formMaildata.put("anonymousFlag", "" + commentContent.getAnonymousFlag());//0:不匿名显示,1:该评论匿名显示
        if (!TextUtils.isEmpty(commentContent.getId())) {
            formMaildata.put("beReplyUser", commentContent.getBeReplyUser());
            formMaildata.put("beReplyNickName", commentContent.getBeReplyNickName());
            formMaildata.put("beReplyAvatar", commentContent.getBeReplyAvatar());
            formMaildata.put("parentId", commentContent.getId());
        }


        Map<String, RequestBody> bodyMap = DataUtils.generateRequestBody(formMaildata);
//        NetUtils.getNetAdapter().toAddCommentString(getOwnerName(), bodyMap, new AbsNetCallBack<String>(String.class) {
//            @Override
//            public void onSucStr(String news) {
//                if (!TextUtils.isEmpty(news)) {
//                    try {
//                        AttentionBean mAttention = gson.fromJson(news, AttentionBean.class);
//                        if (mAttention != null) {
//                            if (null != listener) {
//                                toast("评论成功");
//                                listener.OnAddComment(true);
//                            }
//                        } else {
//                            if (null != listener) {
//                                toast("评论失败");
//                                listener.OnAddComment(false);
//                            }
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        if (null != listener) {
//                            toast("评论失败");
//                            listener.OnAddComment(false);
//                        }
//                    }
//                } else {
//                    onError(null, "数据异常");
//                    if (null != listener) {
//                        toast("评论失败");
//                        listener.OnAddComment(false);
//                    }
//                }
//            }
//
//            @Override
//            public void onError(Throwable ex, String errorMsg) {
////                    toast(errorMsg);
//                if (null != listener) {
//                    toast("评论失败");
//                    listener.OnAddComment(false);
//                }
//            }
//
//            @Override
//            public void onEnd() {
//            }
//        });


    }

    /**
     * 分享
     *
     * @param iShares
     * @param onShareFragmentClickListener
     */
    public void toShareAttentionNews(IShares
                                             iShares, ShareFragment.OnShareFragmentClickListener onShareFragmentClickListener) {
        shareFragment = new ShareFragment();
        shareFragment.setShares(iShares);
        shareFragment.setOnShareFragmentClickListener(onShareFragmentClickListener);
//        shareFragment.setShareSuccessListener(new ShareFragment.ShareSuccess() {
//            @Override
//            public void OnShareSuccess(String shareType) {
//                addInvitation(Constances.ADD_INTEGRAL_FORWARD, false);
//                SkipUtil.skipToAddUserOperate(BaseAccountActivity.this, getOwnerName(), 14, typeName, shareType, newsId, Cache.getInstance().getCurrentChannelId());
//
//            }
//        });
        FragmentManager fm = getSupportFragmentManager();
        shareFragment.show(fm, SHARE_TAG);
        fm.executePendingTransactions();
    }

    public interface OnAttentionListener {
        void OnAttention(boolean isSuccess, long attentionedUserNums);

        void OnUnAttention(boolean isSuccess, long attentionedUserNums);
    }

    public interface OnAddCommentListener {
        void OnAddComment(boolean isSuccess);
    }
}
